﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.IO;
using MediaArtAggregator.Core.SearchProvider;
using MediaArtAggregator.Core.Xml.Serialization;

namespace MediaArtAggregator.Core.Configuration {
  [XmlRoot ( "configuration" )]
  public class Settings {

    public Settings ( ) {
      Types = new Collection<SearchType> ( );
    }
    /// <summary>
    /// Gets or sets the sources.
    /// </summary>
    /// <value>The sources.</value>
    [XmlArray ( "sources" ), XmlArrayItem ( "source" )]
    public Collection<SearchSource> Sources { get; set; }
    /// <summary>
    /// Gets or sets the types.
    /// </summary>
    /// <value>The types.</value>
    [XmlArray ( "types" ), XmlArrayItem ( "type" )]
    public Collection<SearchType> Types { get; set; }

    [XmlElement ( "useragent" )]
    public string UserAgent { get; set; }

    [XmlElement ( "outputimage" )]
    public string OutputFileName { get; set; }

    [XmlElement ( "highlightcolor" )]
    public string HighlightColor { get; set; }

    [XmlElement ( "searchondoubleclick" )]
    public bool SearchOnDoubleClick { get; set; }

    [XmlElement ( "path" )]
    public string Path { get; set; }

    #region Static Methods
    public static Settings Load ( FileInfo file ) {
      Console.WriteLine ( "Load {0}", file.FullName );
      Settings settings = XmlSerializer<Settings>.Deserialize ( file, Utilities.GetSourceProviderTypes ( ) );
      return settings;
    }
    #endregion

    public void Save ( FileInfo file ) {
      FileStream fs = new FileStream ( file.FullName, FileMode.Create, FileAccess.Write, FileShare.Read );
      using ( fs ) {
        XmlSerializer<Settings>.Serialize ( fs, this, Utilities.GetSourceProviderTypes() );
      }
    }
  }
}
